package com.fulei.logic;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class MeiBillValidatorOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  public static boolean ReadInputtedMountFlag = false;
  public static boolean ConsumeSucessOnceFlag = false;
  public static int CardinalNumber = 1;
  public static int[] BillMount = { 5, 10, 20 };
  public static int PollFailTimes = 0;
  
  public static boolean OpenPort()
  {
    serialPort = new SerailPortOpt();
    
    serialPort.mDevNum = Integer.parseInt(comNo);
    serialPort.mSpeed = Integer.parseInt(baudRate);
    serialPort.mSpeed = 9600;
    serialPort.mDataBits = 8;
    serialPort.mStopBits = 1;
    
    serialPort.mParity = 110;
    if (serialPort.mFd == null)
    {
      serialPort.openDev(serialPort.mDevNum);
      serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
      serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    if (serialPort.mFd != null) {
      serialPort.closeDev(serialPort.mFd);
    }
    serialPort.mFd = null;
    serialPort = null;
  }
  
  public static int Reset()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 48, 48 };
      
      LogUtil.writeAcceptCashLog("resetSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      
      serialPort.writeBytes(SendData);
      

      byte[] ConfirmData = new byte[1];
      
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 10, 1);
      

      LogUtil.writeAcceptCashLog("resetRead:" +
        LogUtil.bytesToHexString(ConfirmData, 1));
      if (ConfirmDataLength > 0)
      {
        byte[] ReConfirmData = { 0 };
        
        LogUtil.writeAcceptCashLog("resetWrite:" +
          LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
        
        serialPort.writeBytes(ReConfirmData);
        return 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("Reset:" + ex.getMessage());
    }
    return 0;
  }
  
  public static byte[] GetSetup()
  {
    byte[] Result = null;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 49, 49 };
      LogUtil.writeAcceptCashLog("getSetupSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      serialPort.writeBytes(SendData);
      

      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 200, 10);
      
      LogUtil.writeAcceptCashLog("getSetupRead:" +
        LogUtil.bytesToHexString(ConfirmData, ConfirmData.length));
      if (ConfirmDataLength > 10)
      {
        byte[] ReConfirmData = { 0 };
        
        LogUtil.writeAcceptCashLog("getSetupWrite:" +
          LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
        

        serialPort.writeBytes(ReConfirmData);
        Result = new byte[ConfirmDataLength];
        System.arraycopy(ConfirmData, 0, Result, 0, ConfirmDataLength);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("GetSetup:" + ex.getMessage());
    }
    return Result;
  }
  
  public static byte[] GetBillCount()
  {
    byte[] Result = null;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 54, 54 };
      
      LogUtil.writeAcceptCashLog("getBillCountSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      serialPort.writeBytes(SendData);
      

      byte[] ConfirmData = new byte[10];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 20, 1);
      LogUtil.writeAcceptCashLog("getBillCountRead:" +
        LogUtil.bytesToHexString(ConfirmData, ConfirmData.length));
      if (ConfirmDataLength > 10)
      {
        byte[] ReConfirmData = { 0 };
        LogUtil.writeAcceptCashLog("getBillCountWrite:" +
          LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
        
        serialPort.writeBytes(ReConfirmData);
        Result = new byte[ConfirmDataLength];
        System.arraycopy(ConfirmData, 0, Result, 0, ConfirmDataLength);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("GetBillCount:" + ex.getMessage());
    }
    return Result;
  }
  
  public static int mPoll()
  {
    int ReceCash = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 51, 51 };
      LogUtil.writeAcceptCashLog("pollSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      serialPort.writeBytes(SendData);
      

      byte[] ReConfirmDataS = { 0 };
      serialPort.writeBytes(ReConfirmDataS);
      

      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 20, 1);
      
      LogUtil.writeAcceptCashLog("pollRead:" +
        LogUtil.bytesToHexString(ConfirmData, ConfirmDataLength));
      

      byte[] ReConfirmData = { 0 };
      serialPort.writeBytes(ReConfirmData);
      serialPort.writeBytes(ReConfirmData);
      serialPort.writeBytes(ReConfirmData);
      for (int p = 0; p < ConfirmData.length - 1; p++) {
        if ((ConfirmData[0] == 0) || 
        



          ((ConfirmData[p] & 0xFF) >= 128))
        {
          byte LocationByte = (byte)(ConfirmData[p] & 0x70);
          if (LocationByte == 0)
          {
            if (ReadInputtedMountFlag)
            {
              PollFailTimes = 0;
              byte BillType = (byte)(0xF & ConfirmData[p]);
              ReceCash = BillMount[BillType] * CardinalNumber;
              
              ReadInputtedMountFlag = false;
              return ReceCash;
            }
          }
          else if (LocationByte == 16)
          {
            PollFailTimes = 0;
            
            ReadInputtedMountFlag = true;
            
            mPutBillToBox();
          }
        }
        else if (ConfirmData[p] == 9)
        {
          PollFailTimes += 1;
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("Poll:" + ex.getMessage());
    }
    return 0;
  }
  
  public static int mUseBill()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] Send = { 52, -1, -1, -1, -1, 48 };
      
      LogUtil.writeAcceptCashLog("usebillSend:" +
        LogUtil.bytesToHexString(Send, Send.length));
      
      serialPort.writeBytes(Send);
      

      byte[] ConfirmData = new byte[1];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 10, 1);
      LogUtil.writeAcceptCashLog("usebillRead:" +
        LogUtil.bytesToHexString(ConfirmData, ConfirmData.length));
      if (ConfirmDataLength > 0)
      {
        byte[] ReConfirmData = { 0 };
        LogUtil.writeAcceptCashLog("usebillWrite:" +
          LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
        
        serialPort.writeBytes(ReConfirmData);
        
        return 1;
      }
      return -1;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("UseBill：" + ex.getMessage());
    }
    return 0;
  }
  
  public static int mRobitBill()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] Send = { 52, 0, 0, 0, 0, 52 };
      LogUtil.writeAcceptCashLog("robitBillSend:" +
        LogUtil.bytesToHexString(Send, Send.length));
      serialPort.writeBytes(Send);
      

      byte[] ConfirmData = new byte[1];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 10, 1);
      LogUtil.writeAcceptCashLog("robitBillRead:" +
        LogUtil.bytesToHexString(ConfirmData, ConfirmData.length));
      if (ConfirmDataLength > 1)
      {
        byte[] ReConfirmData = { 0 };
        LogUtil.writeAcceptCashLog("robitBillWrite:" +
          LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
        
        serialPort.writeBytes(ReConfirmData);
        return 1;
      }
      return -1;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("RobitBill:" + ex.getMessage());
    }
    return 0;
  }
  
  public static void mPutBillToBox()
  {
    try
    {
      byte[] Send = { 53, 1, 54 };
      LogUtil.writeAcceptCashLog("putBillToBoxSend:" +
        LogUtil.bytesToHexString(Send, Send.length));
      serialPort.writeBytes(Send);
      
      byte[] ConfirmData = new byte[5];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 10, 1);
      LogUtil.writeAcceptCashLog("putBillToBoxRead:" +
        LogUtil.bytesToHexString(ConfirmData, ConfirmData.length));
      if (ConfirmDataLength > 0)
      {
        byte[] ReConfirmData = { 0 };
        LogUtil.writeAcceptCashLog("putBillToBoxWrite:" +
          LogUtil.bytesToHexString(ReConfirmData, ReConfirmData.length));
        

        serialPort.writeBytes(ReConfirmData);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("PutBillToBox:" + ex.getMessage());
    }
  }
  
  public static int mTemporary()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] Send = { 52, -1, -1, -1, -1, 48 };
      

      serialPort.writeBytes(Send);
      

      Thread.sleep(1L);
      
      byte[] ConfirmData = new byte[1];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 10, 1);
      if (ConfirmDataLength > 1)
      {
        byte[] ReConfirmData = { 0 };
        serialPort.writeBytes(ReConfirmData);
        return 1;
      }
      return -1;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("Temporary:" + ex.getMessage());
    }
    return 0;
  }
  
  public static int mRobittemporary()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] Send = { 52, -1, -1, 0, 0, 50 };
      

      serialPort.writeBytes(Send);
      

      byte[] ConfirmData = new byte[512];
      int ConfirmDataLength = serialPort.readBytes(ConfirmData, 500, 1);
      if (ConfirmDataLength > 1)
      {
        byte[] ReConfirmData = { 0 };
        serialPort.writeBytes(ReConfirmData);
        return 1;
      }
      return -1;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("Robittemporary:" + ex.getMessage());
    }
    return 0;
  }
}
